package com.ruoyi.web.controller.basic;

import java.util.List;

import com.ruoyi.basic.domain.TbExchangeRate;
import com.ruoyi.basic.service.ITbExchangeRateService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.validation.Valid;

/**
 * 汇率Controller
 * 
 * @author ruoyi
 * @date 2022-09-04
 */
@Controller
@RequestMapping("/basic/rate")
public class TbExchangeRateController extends BaseController
{
    private String prefix = "basic/rate";

    @Autowired
    private ITbExchangeRateService tbExchangeRateService;

    @RequiresPermissions("basic:rate:view")
    @GetMapping()
    public String rate()
    {
        return prefix + "/rate";
    }

    /**
     * 查询汇率列表
     */
    @RequiresPermissions("basic:rate:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TbExchangeRate tbExchangeRate)
    {
        startPage();
        List<TbExchangeRate> list = tbExchangeRateService.listByCondition(tbExchangeRate);
        return getDataTable(list);
    }

    /**
     * 导出汇率列表
     */
    @RequiresPermissions("basic:rate:export")
    @Log(title = "汇率", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TbExchangeRate tbExchangeRate)
    {
        List<TbExchangeRate> list = tbExchangeRateService.listByCondition(tbExchangeRate);
        ExcelUtil<TbExchangeRate> util = new ExcelUtil<TbExchangeRate>(TbExchangeRate.class);
        return util.exportExcel(list, "汇率数据");
    }

    /**
     * 新增汇率
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存汇率
     */
    @RequiresPermissions("basic:rate:add")
    @Log(title = "汇率", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated TbExchangeRate tbExchangeRate)
    {
        return toAjax(tbExchangeRateService.saveRate(tbExchangeRate));
    }

    /**
     * 修改汇率
     */
    @RequiresPermissions("basic:rate:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TbExchangeRate tbExchangeRate = tbExchangeRateService.getById(id);
        mmap.put("tbExchangeRate", tbExchangeRate);
        return prefix + "/edit";
    }

    /**
     * 修改保存汇率
     */
    @RequiresPermissions("basic:rate:edit")
    @Log(title = "汇率", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated TbExchangeRate tbExchangeRate)
    {
        return toAjax(tbExchangeRateService.updateRate(tbExchangeRate));
    }

    /**
     * 删除汇率
     */
    @RequiresPermissions("basic:rate:remove")
    @Log(title = "汇率", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tbExchangeRateService.deleteByIds(ids));
    }
}
